const mongoose = require("mongoose");
const db = mongoose.createConnection(
  "mongodb://localhost:27017/TechCodeKnowledgeStructure",
  {
    useNewUrlParser: true,
    useUnifiedTopology: true,
  },
  (err) => {
    if (err) {
      console.log("===============================");
      console.log("数据库连接失败:", err);
      console.log("===============================");
      return;
    }
    console.log("数据库连接成功");
  }
);
//设置数据类型(申明哪个字段类型,哪个集合)
console.log(db);
const Model = db.model("api", {
  uname: { type: String, default: "武兴师" },
  sex: { type: String },
  pwd: { type: String },
  age: Number,
});
//实例化
const obj = new Model({
  uname: "wxs",
  pwd: "admin123",
  age: 20,
  sex: "男",
});
//创建实例操作
obj
  .save()
  .then((res) => console.log(res))
  .catch((err) => console.log(err));
